<link rel="stylesheet" type="text/css" href="<?php echo $this->basePath('public/css/dbblack/js/form/style.css'); ?>"/>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/form/form.js'); ?>"></script>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/artdialog/artDialog.js?skin=simple'); ?>"></script>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/artdialog/plugins/iframeTools.js'); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $this->basePath('public/css/dbblack/css/simple.css'); ?>"/>
<script type='text/javascript' src="<?php echo $this->basePath('public/css/dbblack/js/jquery.form.js'); ?>"></script>
<div class="container">
	<div class="order-info mt20">
		<form action="<?php echo $this->url('frontcart/default', array('action'=>'step')); ?>" onsubmit="return check_address_id();" method="post">
			<div class="clearfix address address-box">
				<h2><b class="fl"><?php echo $this->translate('选择收货地址'); ?>：</b><a class="btn btn-main btn-mini fr" href="<?php echo $this->url('frontaddress/default'); ?>" target="_blank"><?php echo $this->translate('管理地址'); ?></a></h2>
				<ul class="address-list clearfix">
					<?php if ($this->address_list) { foreach ($this->address_list as $address_value) { ?>
					<li <?php if ($address_value['addr_default'] == 1) echo 'class="selected""'; ?>>
						<a href="javascript:;" data-value="<?php echo $address_value['address_id']; ?>" class="modify"> <?php echo $this->translate('修改地址'); ?> </a>
						<div class="address-info " >
							<input type="radio" name="user_address_id" value="<?php echo $address_value['address_id']; ?>" <?php if ($address_value['addr_default'] == 1) echo 'checked="checked"'; ?>>
							<label>
								<?php
								$region_array = @explode(' ', $address_value['region_value']);
								$region_array = array_reverse($region_array);
								echo $this->escapeHtml(implode(', ', $region_array));
								?>
								（<?php echo $this->escapeHtml($address_value['true_name']);?> <?php echo $this->translate('收'); ?>）
							</label>
							<p>
								<?php echo $this->escapeHtml($address_value['address']); ?><br>
							</p>
						</div>
						<i class="icon-selected-32 ie6png"></i>
					</li>
					<?php } } else { ?>
						<INPUT type="hidden" name="address_empty" id="address_empty" value='empty'>
					<?php } ?>
				</ul>
				<div><a id="address_other" class="btn btn-main btn-mini" href="javascript:;"><?php echo $this->translate('使用新地址'); ?></a></div>
			</div>
			<div class=" clearfix mt10">
				<p style="text-align: center;">
					<input type="hidden" name="step" value="setaddress" />
					<input type="button" class="btn" style="vertical-align:bottom;" onClick="location.href='<?php echo $this->url('frontcart/default'); ?>'" value="<?php echo $this->translate('修改购物车'); ?>">&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="submit" class="btn btn-main" value="<?php echo $this->translate('进入下一步'); ?>">
				</p>
			</div>
		</form>
	</div>
</div>

<form method="post" class="simple" id="address-form" style="display: none;" novalidate="true">
	<input type="hidden" name="address_id" id="address_id" value="">
	<table class="form">
		<tbody>
		<tr>
			<td class="label"><?php echo $this->translate('所在地区'); ?><span class="must_add_value">*</span>：</td>
			<td>
				<div class="controls" id="region">
					<span id="show_region_value"></span>
					<select id="show_address_area">
						<option value=""><?php echo $this->translate('请选择'); ?></option>
						<?php if ($this->region_array and !empty($this->region_array )) {
							foreach($this->region_array as $value) {
								?>
								<option value="<?php echo $value['region_id']; ?>"><?php echo $value['region_name']; ?></option>
								<?php
							}
						}
						?>
					</select>
					<input type="hidden" name="region_id" id="region_id" value="" class="region_ids">
					<input type="hidden" name="region_value" id="region_value" value="" class="region_names" />
				</div>
			</td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('邮政编码'); ?><span class="must_add_value">*</span>：</td><td><input type="text" value="" placeholder="<?php echo $this->translate('填写邮政编码'); ?>"  name="zip_code" id="zip_code" ></td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('街道地址'); ?><span class="must_add_value">*</span>：</td><td><textarea placeholder="<?php echo $this->translate('填写街道详细地址'); ?>" maxlen="120" minlen="5"  name="address" id="address"></textarea> <label>&nbsp;</label></td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('收货人姓名'); ?><span class="must_add_value">*</span>：</td><td><input type="text"  placeholder="<?php echo $this->translate('填写收货人姓名'); ?>" value="" name="true_name" id="true_name"> <label></label></td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('手机号码'); ?><span class="must_add_value">*</span>：</td><td><input type="text" value="" name="mod_phone" id="mod_phone" placeholder="<?php echo $this->translate('添加手机号码'); ?>" inform="0"><label></label></td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('电话号码'); ?>：</td>
			<td><input type="text" empty="" placeholder="<?php echo $this->translate('区号'); ?>" value="" name="tel_area_code" id="tel_area_code" inform="0" style="width: 50px;"> - <input type="text" empty="" value="" placeholder="<?php echo $this->translate('电话'); ?>" name="tel_phone" id="tel_phone" inform="0" style="width: 100px;"> - <input type="text" empty="" value="" placeholder="<?php echo $this->translate('分机'); ?>" name="tel_ext" id="tel_ext" inform="0" style="width: 60px;"><label></label>
			</td>
		</tr>
		<tr>
			<td class="label"><?php echo $this->translate('设为默认地址'); ?>：</td><td><input type="checkbox" value="1" name="addr_default" id="addr_default"><label><?php echo $this->translate('设置为默认收货地址'); ?></label></td>
		</tr>
		<tr>
			<td class="tc" colspan="2">
				<input type="hidden" name="address_security" value="<?php echo $this->address_csrf['value']; ?>">
				<button type="button" onclick="address_save_js();" id="address_add_submit" class="btn"><?php echo $this->translate('保存修改'); ?></button>
			</td>
		</tr>
		</tbody></table>
</form>
<script>
	//收货地址保存
	function address_save_js() {
		if($('#region_id').val() == '') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('地区不能为空！'); ?></p>");
			return false;
		}
		if($('#zip_code').val() == '') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('邮政编码不能为空！'); ?></p>");
			return false;
		}
		if($('#address').val() == '') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('街道地址不能为空！'); ?></p>");
			return false;
		}
		if($('#true_name').val() == '') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('收货人不能为空！'); ?></p>");
			return false;
		}
		if($('#mod_phone').val() == '') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('手机号码不能为空！'); ?></p>");
			return false;
		}

		$.post("<?php echo $this->url('frontaddress/default',array('action'=>'saveaddress')); ?>",$('#address-form').formSerialize(),
				function(data){
					if(data != 'true') {
						alert(data);
					}
					window.location.href="<?php echo $this->url('frontcart/default', array('action'=>'setaddress')); ?>";
				});
	}
	/**
	 * 检查是否为空
	 */
	function check_address_id() {
		if($('#address_empty').val() == 'empty') {
			art.dialog.tips("<p class='warning'><?php echo $this->translate('请添加收货地址！'); ?></p>");
			return false;
		} else {
			var val_address= $('input:radio[name="user_address_id"]:checked').val();
			if(val_address == null) {
				art.dialog.tips("<p class='warning'><?php echo $this->translate('请选择收货地址！'); ?></p>");
				return false;
			} else {
				return true;
			}
			return true;
		}
	}

	var dlg;
	$("#address_other").on("click",function(){
		$('#address-form').resetForm();
		$('#address_id').val('');

		$('#show_address_area').show();
		$('#show_region_value').hide();

		dlg = dialog({id:'address_form_id',opacity:0.3,padding:'20px 10px 10px 20px',width:900,title:'<?php echo $this->translate('收货地址'); ?>',content:document.getElementById('address-form'),lock:true});
		dlg.showModal();
	});
	$(".address-list .modify").each(function(){

		$(this).on("click",function(){
			var address_id = $(this).attr("data-value");
			if(address_id != '') {
				$('#address-form').resetForm();

				$.post('<?php echo $this->url('frontaddress/default', array('action'=>'editAddress')); ?>', {'address_id':address_id}, function(data){
							if (data)
							{
								$('#show_address_area').hide();
								$('#show_region_value').show();

								$('#address_id').val(data.address_id);
								$('#true_name').val(data.true_name);
								$('#region_id').val(data.region_id);
								$('#region_value').val(data.region_value);
								$('#address').val(data.address);
								$('#zip_code').val(data.zip_code);
								$('#mod_phone').val(data.mod_phone);
								$('#tel_area_code').val(data.tel_area_code);
								$('#tel_phone').val(data.tel_phone);
								$('#tel_ext').val(data.tel_ext);

								if(data.default_value == 1) {
									$('#addr_default').attr('checked',true);
								} else {
									$('#addr_default').attr('checked',false);
								}

								$('#show_region_value').html(data.region_value+'<input type="button" value="<?php echo $this->translate('编辑'); ?>" class="edit_region" onclick="regionEdit();" />');

								dlg = dialog({id:'address_form_id',opacity:0.3,padding:'20px 10px 10px 20px',width:900,title:'<?php echo $this->translate('收货地址'); ?>',content:document.getElementById('address-form'),lock:true});
								dlg.showModal();
							}
						},
						'json');
			}
			$('.db_show_area').hide();
			return false;
		});

	});

	$(".address-list li").each(function(){
		var val_address= $('input:radio[name="user_address_id"]:checked').val();
		if(val_address == '') {
			$(this).has("input[name='user_address_id']:checked").addClass("selected");
		}
		$(this).on("click",function(){
			$(".address-list li").removeClass("selected");
			$("input[name='user_address_id']").removeProp("checked");
			$("input[name='user_address_id']",this).prop("checked","checked");
			$(this).addClass("selected");
		});
	});

	var SITE_REGION_URL  = '<?php echo $this->url('frontaddress/default',array('action'=>'selectArea','region_type'=>'region_top_id')); ?>';
	var AREA_SELECT_LANG = '<?php echo $this->translate('请选择'); ?>';
	regionInit("region");

	/* 地区选择函数 */
	function regionInit(divId)
	{
		$("#" + divId + " > select").change(regionChange); // select的onchange事件
	}

	function regionChange()
	{
		// 删除后面的select
		$(this).nextAll("select").remove();
		// 计算当前选中到id和拼起来的name
		var selects = $(this).siblings("select").andSelf();
		var id = 0;
		var i;
		var names = new Array();
		for (i = 0; i < selects.length; i++)
		{
			sel = selects[i];
			if (sel.value > 0)
			{
				id = sel.value;
				name = sel.options[sel.selectedIndex].text;
				names.push(name);
			}
		}
		$(".region_ids").val(id);
		$(".region_names").val(names.join(" "));
		// ajax请求下级地区
		if (this.value > 0)
		{
			var _self = this;
			var url = SITE_REGION_URL;
			$.post(url, {'region_id':this.value}, function(data){
						if (data)
						{
							if (data.length > 0)
							{
								$("<select class='db_show_area'><option>"+AREA_SELECT_LANG+"</option></select>").change(regionChange).insertAfter(_self);
								var data  = data;
								for (i = 0; i < data.length; i++)
								{
									$(_self).next("select").append("<option value='" + data[i].region_id + "'>" + data[i].region_name + "</option>");
								}
							}
						}
					},
					'json');
		}
	}
	function regionEdit()
	{
		$("#show_address_area").show();
		$("#show_region_value").hide();
	}
</script>